/*
 ------------------------------------------------------------------------------
 Copyright (C) 2006-2007 Team Blur.

 This file is part of the Quantum Engine source code.

 The Quantum Engine source code is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or (at your
 option) any later version.

 The Quantum Engine source code is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 more details.

 You should have received a copy of the GNU General Public License along with
 the Quantum Engine source code; if not, write to the Free Software Foundation, Inc.,
 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 ------------------------------------------------------------------------------
*/


#include "gamelib.h"


short		(*LittleShort)(short s);
short		(*BigShort)(short s);
int			(*LittleLong)(int l);
int			(*BigLong)(int l);
float		(*LittleFloat)(float f);
float		(*BigFloat)(float f);


/*
 ==================
 ShortSwap
 ==================
*/
static short ShortSwap (short s){

	union {
		byte	b[2];
		short	s;
	} in, out;

	in.s = s;

	out.b[0] = in.b[1];
	out.b[1] = in.b[0];

	return out.s;
}

/*
 ==================
 ShortNoSwap
 ==================
*/
static short ShortNoSwap (short s){

	return s;
}

/*
 ==================
 LongSwap
 ==================
*/
static int LongSwap (int l){

	union {
		byte	b[4];
		int		l;
	} in, out;

	in.l = l;

	out.b[0] = in.b[3];
	out.b[1] = in.b[2];
	out.b[2] = in.b[1];
	out.b[3] = in.b[0];

	return out.l;
}

/*
 ==================
 LongNoSwap
 ==================
*/
static int LongNoSwap (int l){

	return l;
}

/*
 ==================
 FloatSwap
 ==================
*/
static float FloatSwap (float f){

	union {
		byte	b[4];
		float	f;
	} in, out;
	
	in.f = f;

	out.b[0] = in.b[3];
	out.b[1] = in.b[2];
	out.b[2] = in.b[1];
	out.b[3] = in.b[0];
	
	return out.f;
}

/*
 ==================
 FloatNoSwap
 ==================
*/
static float FloatNoSwap (float f){

	return f;
}

/*
 ==================
 Swap_IsLittleEndian
 ==================
*/
bool Swap_IsLittleEndian (void){

	byte	swapTest[2] = {1, 0};

	return (*(short *)swapTest == 1);
}

/*
 ==================
 Swap_IsBigEndian
 ==================
*/
bool Swap_IsBigEndian (void){

	byte	swapTest[2] = {1, 0};

	return (*(short *)swapTest != 1);
}

/*
 ==================
 Swap_Init
 ==================
*/
void Swap_Init (void){

	byte	swapTest[2] = {1, 0};

	// Set the byte swapping variables in a portable manner
	if (*(short *)swapTest == 1){
		LittleShort = ShortNoSwap;
		BigShort = ShortSwap;
		LittleLong = LongNoSwap;
		BigLong = LongSwap;
		LittleFloat = FloatNoSwap;
		BigFloat = FloatSwap;
	}
	else {
		LittleShort = ShortSwap;
		BigShort = ShortNoSwap;
		LittleLong = LongSwap;
		BigLong = LongNoSwap;
		LittleFloat = FloatSwap;
		BigFloat = FloatNoSwap;
	}
}
